home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / fontmaker / bmfd / getopt.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  3KB  |  134 lines

  1. /* $Id: getopt.c,v 1.2 1993/06/11 16:31:57 Rhialto Exp $
  2.  * $Log: getopt.c,v $
  3.  * Revision 1.2  1993/06/11  16:31:57  Rhialto
  4.  * First real RCS checkin
  5.  *
  6.  */
  7. /*
  8.  * getopt.h - Get next option letter from argument vector. v1.1
  9.  * 12-Dec-1987    aklevin
  10.  */
  11.  
  12. #define GETOPT_H
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. /*
  18.  * optarg points to an option's argument (if any). optind holds the index
  19.  * of the next argument vector element to parse. Once all options have
  20.  * been parsed, points to the first non-option argument. [If (optind >
  21.  * argc) then there are no more arguments]. opterr, if set to 0 will
  22.  * suppress getopt's error messages (default is 1). optopt, while not
  23.  * usually documented, is used here to return the actual option character
  24.  * found, even when getopt itself returns '?'.
  25.  */
  26. char           *optarg;
  27. int        optind = 1,
  28.         opterr = 1,
  29.         optopt;
  30.  
  31. int
  32. getopt(argc, argv, optstring)
  33. int        argc;
  34. char           *argv[],
  35.            *optstring;
  36. {
  37.  
  38.     int         any_more,
  39.             i,
  40.             result;
  41.     static int        opthold,
  42.             optsub = 1;
  43.  
  44.     /* Reset optarg upon entry    */
  45.     optarg = NULL;
  46.  
  47.     /* Reset optsub if caller has changed optind.     */
  48.     if (optind != opthold)
  49.     optsub = 1;
  50.  
  51.     /* Look at each element of the argument vector still unparsed.  */
  52.     for (; optind < argc; optind++) {
  53.     /*
  54.      * Done if a non-option argument or single dash is reached.
  55.      * However, don't skip over said argument.
  56.      */
  57.     if (argv[optind][0] != '-' || argv[optind][1] == '\0')
  58.         break;
  59.  
  60.     /* Got an option.  */
  61.  
  62.     /* Done if "--" is reached.  Skip over it, too.  */
  63.     if (argv[optind][1] == '-') {
  64.         optind++;
  65.         break;
  66.     }
  67.     /* Look at each character in optstring.  */
  68.     for (i = 0; i < strlen(optstring); i++) {
  69.         if ((optopt = argv[optind][optsub]) != optstring[i])
  70.         continue;
  71.  
  72.         /* Got a match.  */
  73.  
  74.         /* Are there any more chars in this option?  e.g. `-abc'  */
  75.         any_more = strlen(argv[optind]) - optsub - 1;
  76.  
  77.         /* Does this option require an argument?  */
  78.         if (optstring[i + 1] == ':') {
  79.  
  80.         /* Yes.  If this is the last argument, complain.  */
  81.         if (optind == argc - 1 && !any_more) {
  82.             if (opterr)
  83.             fprintf(stderr, "%s: `-%c' option requires an argument.\n", argv[0], optopt);
  84.             optind++;
  85.             result = '?';
  86.             goto leave;
  87.         }
  88.         /* end if (opt */
  89.         /*
  90.          * Qualifier is either rest of this argument (if any) or
  91.          * next argument.
  92.          */
  93.         else {
  94.             if (!any_more)
  95.             optarg = argv[++optind];
  96.             else
  97.             optarg = &argv[optind][optsub + 1];
  98.             optind++;
  99.             optsub = 1;
  100.         }        /* end else */
  101.         }
  102.          /* end if (opt */
  103.         else {
  104.         /* No argument; just adjust indices.  */
  105.         /* Advance to next argument.  */
  106.         if (!any_more) {
  107.             optind++;
  108.             optsub = 1;
  109.         }
  110.         /* end if (! */
  111.          /* Advance to next character.     */
  112.         else
  113.             optsub++;
  114.         }            /* end else */
  115.         result = optopt;
  116.         goto leave;
  117.     }            /* end for (i=0 */
  118.     if (opterr)
  119.         fprintf(stderr, "%s: Unrecognized option `-%c'.\n", argv[0], optopt);
  120.     if (strlen(argv[optind]) - optsub - 1)
  121.         optsub++;
  122.     else {
  123.         optind++;
  124.         optsub = 1;
  125.     }
  126.     result = '?';
  127.     goto leave;
  128.     }                /* end for ( ; */
  129.     result = EOF;
  130. leave:
  131.     opthold = optind;
  132.     return (result);
  133. }                /* end getopt() */
  134.